package com.erp.web.controller.framework;


import com.alibaba.fastjson.JSONArray;
import com.erp.framework.config.GlobalConfig;
import com.erp.framework.constant.ConfigConstants;
import com.erp.framework.core.controller.BaseController;
import com.erp.framework.domain.SysNotice;
import com.erp.framework.domain.SysUser;
import com.erp.framework.service.ISysConfigService;
import com.erp.framework.service.ISysMenuService;
import com.erp.framework.service.ISysNoticeService;
import com.erp.framework.util.CookieUtils;
import com.erp.framework.util.DateUtils;
import com.erp.framework.util.DictUtils;
import com.erp.framework.util.ShiroUtils;
import com.erp.system.domain.ToDo;
import com.erp.system.service.IToDoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletResponse;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 首页 业务处理
 *
 * @author erp
 */
@Controller
public class SysIndexController extends BaseController {

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private IToDoService toDoService;

    @Autowired
    private ISysNoticeService noticeService;

    // 系统首页
    @GetMapping("/index")
    public String index(ModelMap mmap) {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        // 取出菜单
        mmap.put("menus", menuService.selectMenuList());
        mmap.put("user", user);
        mmap.put("sideTheme", configService.selectConfigByKey("sys.index.sideTheme"));
        mmap.put("skinName", configService.selectConfigByKey("sys.index.skinName"));
        mmap.put("ignoreFooter", configService.selectConfigByKey("sys.index.ignoreFooter"));
        mmap.put("copyrightYear", GlobalConfig.getCopyrightYear());

        return "index";
    }

    // 切换主题
    @GetMapping("/framework/switchSkin")
    public String switchSkin() {
        return "skin";
    }

    // 切换菜单
    @GetMapping("/framework/menuStyle/{style}")
    public void menuStyle(@PathVariable String style, HttpServletResponse response) {
        CookieUtils.setCookie(response, "nav-style", style);
    }

    // 系统介绍
    @GetMapping("/framework/main")
    public String main(ModelMap modelMap) {
        List<ToDo> todoList = new ArrayList<>();
        // 获取身份信息
        SysUser user = ShiroUtils.getSysUser();
        // 获取到所有的系统通知
        List<ToDo> list = toDoService.queryAll();
        // 获取未关闭的通知和公告
        SysNotice param = new SysNotice();
        param.setStatus(DictUtils.getDictValue("sys_notice_status", ConfigConstants.ENABLE));
        List<SysNotice> noticeList = noticeService.list(param);

        //  个人总数 个人待办
        int TotalNum = 0, toDoNum = 0;
        // 周总数  周完成
        int weekNum = 0, weekDone = 0;
        // 月总数  月完成
        int monthNum = 0, monthDone = 0;
        // 今日新增 日完成
        int dayNum = 0, dayDone = 0;

        Date day = DateUtils.calculateDays(0);
        Date week = DateUtils.calculateWeeks(-1);
        Date month = DateUtils.calculateMonths(-1);
        // 循环处理出有效的数据
        for (ToDo toDo : list) {
            // 处理个人的数据
            if (user.getId().equals(toDo.getUserId())) {
                // 累加
                TotalNum++;
                // 月
                if (toDo.getCreateTime().after(month)) {
                    monthNum++;
                    // 周
                    if (toDo.getCreateTime().after(week)) {
                        weekNum++;
                        // 日
                        if (toDo.getCreateTime().after(day)) {
                            dayNum++;
                            if (!ConfigConstants.TODO.equals(toDo.getStatus())) {
                                dayDone++;
                            }
                        }
                        if (!ConfigConstants.TODO.equals(toDo.getStatus())) {
                            weekDone++;
                        }
                    }
                    if (!ConfigConstants.TODO.equals(toDo.getStatus())) {
                        monthDone++;
                    }
                }
                if (ConfigConstants.TODO.equals(toDo.getStatus())) {
                    toDoNum++;
                    todoList.add(toDo);
                }
            }
        }

        // 日完成率
        String daily = "100.00";
        // 周完成率
        String weekly = "100.00";
        // 月完成率
        String monthly = "100.00";

        DecimalFormat decimalFormat = new DecimalFormat("#.00");
        // 计算
        if (TotalNum > 0) {
            monthly = "0.00";
            weekly = "0.00";
            daily = "0.00";
            if (monthNum > 0) {
                monthly = decimalFormat.format(monthDone * 100.00 / monthNum);
                if (weekNum > 0) {
                    weekly = decimalFormat.format(weekDone * 100.00 / weekNum);
                    if (dayNum > 0) {
                        daily = decimalFormat.format(dayDone * 100.00 / dayNum);
                    }

                }
            }
        }

        modelMap.put("toDoNum", toDoNum);
        modelMap.put("dayNum", dayNum);
        modelMap.put("daily", daily);
        modelMap.put("weekly", weekly);
        modelMap.put("monthly", monthly);
        modelMap.put("todoList", JSONArray.toJSONString(todoList));
        modelMap.put("noticeList", JSONArray.toJSONString(noticeList));

        return "main";
    }

}
